/*
 * scrape-controller.js
 */

if (typeof ScraperController == "undefined") {
	var ScraperController = {
		_log : new Logger("ScraperController"),
		scrapeTask : window.arguments[0],
		queue : new Array(),
		curIndex : 0,
		_hash : {},
		_visited : {},
		count : 0,
		isFirstPage : true,
		scraper : null,
		topic : null,
		threadCount : 0,
		paused : false
	};

	ScraperController.load = function() {
		try {
			ScraperController._log.debug("> load controller");

			$("total-page-num").value = LocaleUtils.getFormattedString(
					"total_page_num", [1]);
			$("total-thead-num").value = LocaleUtils.getFormattedString(
					"total_thread_num", [0]);

			var scrapeTask = ScraperController.scrapeTask;

			// add the first url into the queue and hash it.
			var url = scrapeTask.url;
			this.queue.push(url);
			this._hash[url] = true;

			if (scrapeTask.forumName == "sohu")
				ScraperController.scraper = Sohu_Scraper;
			else if (scrapeTask.forumName == "tianya")
				ScraperController.scraper = Tianya_Scraper;

			$("pageBrowser").preferences.getBranch(null).setIntPref(
					"permissions.default.image", 2);

			// start loading pages.
			ScraperController.loadDoc();

			$("pageBrowser").addProgressListener(iForumListener,
					Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
		} catch (e) {
			ScraperController._log.error("loading controller: " + e);
		}
	};

	ScraperController.unload = function() {
		$("pageBrowser").preferences.getBranch(null).setIntPref(
				"permissions.default.image", 1);
	}

	ScraperController.loadDoc = function() {
		try {

			var curIndex = ScraperController.curIndex;
			var queue = ScraperController.queue;
			var url = queue[curIndex];

			ScraperController.startScrappingPage(url);

			this._log.debug("loading page: " + url);
			$("pageBrowser").setAttribute("src", url);
		} catch (e) {
			ScraperController._log.error("loadDoc: " + e);
		}
	};

	ScraperController.loadComplete = function() {
		try {
			var doc = $("pageBrowser").contentWindow.document;
			if (doc && ScraperController.scraper) {
				var waitTime = ScraperController.scraper.waitTime;
				if (waitTime <= 0 || doc.has_set_timeout) {
					// waitTime <= 0 or has been time out.
					var doc_url = doc.location.href + "";
					if (ScraperController._visited[doc_url]) {
						// loaded document has been visited.
						return;
					}
					ScraperController._log.debug("start scraping...");
					ScraperController._visited[doc_url] = true;

					var data = new Object();
					ScraperController.scraper.execute(doc, data,
							ScraperController.isFirstPage);

					ScraperController.curIndex++;
					for (var i = 0; i < data.pages.length; i++) {
						var url = data.pages[i];
						if (!ScraperController._hash[url]) {
							ScraperController._hash[url] = true;
							ScraperController.queue.push(url);
						}
					}

					// update data base.
					var isFirstPage = ScraperController.isFirstPage;
					if (isFirstPage) {
						ScraperController.topic = iForumDAO.createTopic(
								data.topic.title, data.topic.author,
								data.topic.createtime, data.topic.url,
								ScraperController.scrapeTask.folderid, 0, 0,
								-1, 0);
						ScraperController.isFirstPage = false;
					}

					var topicid = ScraperController.topic.id;
					for (var i = 0; i < data.threads.length; i++) {
						var thread = data.threads[i];
						var dbThread = iForumDAO.createThread(
								ScraperController.threadCount, thread.title,
								thread.author, thread.createtime,
								thread.content, 0, topicid);
						ScraperController.threadCount++;
						if (i == 0 && isFirstPage) {
							// update topic's main thread id
							ScraperController.topic.mainthreadid = dbThread.id;
						}
					}

					ScraperController.topic.reply = ScraperController.threadCount;
					iForumDAO.updateTopic(ScraperController.topic);

					// update the ui elements.
					$("total-thead-num").value = LocaleUtils
							.getFormattedString("total_thread_num",
									[ScraperController.threadCount]);
					$("total-page-num").value = LocaleUtils.getFormattedString(
							"total_page_num", [ScraperController.queue.length]);
					ScraperController.endScrappingPage(doc);
					if (ScraperController.curIndex < ScraperController.queue.length) {
						$("scrape-progmeter").value = (ScraperController.curIndex * 100.0)
								/ ScraperController.queue.length;
						ScraperController.loadDoc();
					} else {
						$("scrape-progmeter").value = 100;
						$("scrapping-status").value = LocaleUtils
								.getString("iforum_scrape_finished");
						$("pageBrowser").preferences.getBranch(null)
								.setIntPref("permissions.default.image", 1);
					}
				} else {
					ScraperController._log.debug("setTimeout " + waitTime);
					doc.has_set_timeout = true;
					window.setTimeout("ScraperController.loadComplete",
							waitTime);
				}
			}
		} catch (e) {
			ScraperController._log.error("loadDoc: " + e);
		}
	};

	ScraperController.startScrappingPage = function(url) {
		var idx = ScraperController.curIndex + 1;
		var val = url;
		if (val.length > 60) {
			val = val.substr(0, 45) + "..."
					+ val.substr(val.length - 15, val.length);
		}
		var title = LocaleUtils.getString("fetching_title");
		var key = "scrape_status_working";

		var listitem = ScraperController.createListItem(idx, title, val, key);
		$("scrape-pages").appendChild(listitem);
	};

	ScraperController.endScrappingPage = function(doc) {
		var idx = ScraperController.curIndex;
		var val = doc.location.href + "";
		if (val.length > 60) {
			val = val.substr(0, 45) + "..."
					+ val.substr(val.length - 15, val.length);
		}
		var title = doc.title;
		var key = "scrape_status_finish";
		var listitem = ScraperController.createListItem(idx, title, val, key);
		var lastitem = $("scrape-pages").lastChild;
		$("scrape-pages").replaceChild(listitem, lastitem);
	};

	ScraperController.createListItem = function(idx, title, url, key) {
		var listitem = document.createElement("listitem");

		var label = document.createElement("label")
		label.setAttribute("value", idx);
		listitem.appendChild(label);

		label = document.createElement("label")
		label.setAttribute("value", title);
		listitem.appendChild(label);

		label = document.createElement("label")
		label.setAttribute("value", url);
		listitem.appendChild(label);

		label = document.createElement("label")
		label.setAttribute("value", LocaleUtils.getString(key));
		listitem.appendChild(label);
		return listitem;
	};

	ScraperController.exit = function() {
		$("pageBrowser").preferences.getBranch(null).setIntPref(
				"permissions.default.image", 1);
	};
}